<?php


namespace app\index\controller;

use app\common\model\Comment;

class CommentController{
    public function add(){
        $userData = cookie('userData');
        if(!$userData){
            return json(array('type' => 1, 'msg' => '未登录'));
        }

        $uid =$userData['uid'];
        $content = trim(input('post.content'));
        $nid = trim(input('post.nid'));

        if ($content == '') {
            return json(array('type' => 1, 'msg' => '评论内容必须填写'));
        }

        $commentModel = new Comment();
        $commentModel->add($uid, $content, $nid);

        return json(array('type'=>0,'msg'=>'评论成功'));
    }


    public function getPageCommentList(){
        $nid = trim(input('get.nid'));
        $page = trim(input('get.page',1));
        $limit = trim(input('get.limit',2));

        $commentModel = new Comment();
        $commentList = $commentModel->getPageCommentByNid($nid, ($page-1)*$limit, $limit);
        foreach($commentList as $key => $comment){
            $commentList[$key]['content'] = htmlspecialchars($comment['content']);
        }
        $count = $commentModel->getCommentCountByNid($nid)[0]['totalCount'];

        return json(array('type'=>0,'commentList'=>$commentList,'count'=>$count,'page'=>$page,'limit'=>$limit));
    }
}